/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#include "stdafx.h"
#include "WZObjectCollection.h"

using namespace std;

namespace WZLib{
	ctor WZObjectCollection::WZObjectCollection(){
	}
	dtor WZObjectCollection::~WZObjectCollection(){
		for(unsigned int i=0,l=this->_vec.size();i<l;i++)
			delete this->_vec[i];
		this->clear();
	}
	WZObject* WZObjectCollection::push_back(WZLib::WZObject* obj){
		return this->add(obj);
	}
	WZObject* WZObjectCollection::push_backNODUP(WZLib::WZObject* obj){
		if(this->index(obj)==-1)
			this->push_back(obj);
		return obj;
	}
	WZObject* WZObjectCollection::add(WZLib::WZObject *obj){
		this->_vec.push_back(obj);
		return obj;
	}
	WZObject* WZObjectCollection::operator [](int i){
		return this->at(i);
	}
	WZObject* WZObjectCollection::at(int i){
		if(i<0 || i>(this->_vec.size()-1))
			return NULL;
		return this->_vec.at(i);
	}
	WZObject* WZObjectCollection::operator [](string name){
		return this->at(name);
	}
	WZObject* WZObjectCollection::at(string name){
		for(unsigned int i=0,l=this->_vec.size();i<l;i++)
			if(this->_vec.at(i)->name()==name)
				return this->_vec.at(i);
		return NULL;
	}
	WZObject* WZObjectCollection::operator [](WZObject* obj){
		return this->at(obj);
	}
	WZObject* WZObjectCollection::at(WZLib::WZObject *obj){
		for(unsigned int i=0,l=this->_vec.size();i<l;i++)
			if(this->_vec.at(i)==obj)
				return obj;
		return NULL;
	}
	int WZObjectCollection::index(WZLib::WZObject* obj){
		for(unsigned int i=0,l=this->_vec.size();i<l;i++)
			if(this->_vec.at(i)==obj)
				return i;
		return -1;
	}
	unsigned int WZObjectCollection::size(){
		return this->_vec.size();
	}
	void WZObjectCollection::clear(){
		this->_vec.clear();
	}
};